It shows how to enumerate one or more cameras connected to the self-developed frame grabber via enumeration APIs, and perform operations such as turning on cameras and grabbing images.
9 currentsystem = platform.system()
10 if currentsystem ==
'Windows':
11 sys.path.append(os.path.join(os.getenv(
'MVCAM_COMMON_RUNENV'),
"Samples",
"Python",
"MvImport"))
13 sys.path.append(os.path.join(
"..",
"..",
"MvImport"))
15 from MvCameraControl_class
import *
19 if sys.version_info[0] < 3:
21 input_func = raw_input
29 Safely decode a string from a ctypes character array. 30 Compatible with Python 2.x and 3.x, as well as 32-bit and 64-bit environments. 32 byte_str = memoryview(ctypes_char_array).tobytes()
35 null_index = byte_str.find(b
'\x00')
37 byte_str = byte_str[:null_index]
40 for encoding
in [
'gbk',
'utf-8',
'latin-1']:
42 return byte_str.decode(encoding)
43 except UnicodeDecodeError:
47 return byte_str.decode(
'latin-1', errors=
'replace')
51 stFrameInfo = POINTER(MV_FRAME_OUT_INFO_EX)
52 pData = POINTER(c_ubyte)
53 FrameInfoCallBack =
fun_ctype(
None, pData, stFrameInfo, c_void_p)
57 stFrameInfo = cast(pFrameInfo, POINTER(MV_FRAME_OUT_INFO_EX)).contents
59 print(
"get one frame: Width[%d], Height[%d], nFrameNum[%d]" % (stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nFrameNum))
65 for i
in range(0, deviceList.nDeviceNum):
66 mvcc_dev_info = cast(deviceList.pDeviceInfo[i], POINTER(MV_CC_DEVICE_INFO)).contents
67 if mvcc_dev_info.nTLayerType == MV_GIGE_DEVICE
or mvcc_dev_info.nTLayerType == MV_GENTL_GIGE_DEVICE:
68 print (
"\ngige device: [%d]" % i)
69 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stGigEInfo.chModelName)
70 print (
"device model name: %s" % strModeName)
72 nip1 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24)
73 nip2 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16)
74 nip3 = ((mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8)
75 nip4 = (mvcc_dev_info.SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff)
76 print (
"current ip: %d.%d.%d.%d\n" % (nip1, nip2, nip3, nip4))
77 elif mvcc_dev_info.nTLayerType == MV_USB_DEVICE:
78 print (
"\nu3v device: [%d]" % i)
79 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chModelName)
80 print (
"device model name: %s" % strModeName)
82 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stUsb3VInfo.chSerialNumber)
83 print (
"user serial number: %s" % strSerialNumber)
84 elif mvcc_dev_info.nTLayerType == MV_GENTL_CAMERALINK_DEVICE:
85 print (
"\nCML device: [%d]" % i)
86 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chModelName)
87 print (
"device model name: %s" % strModeName)
89 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCMLInfo.chSerialNumber)
90 print (
"user serial number: %s" % strSerialNumber)
91 elif mvcc_dev_info.nTLayerType == MV_GENTL_CXP_DEVICE:
92 print (
"\nCXP device: [%d]" % i)
93 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chModelName)
94 print (
"device model name: %s" % strModeName)
96 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stCXPInfo.chSerialNumber)
97 print (
"user serial number: %s" % strSerialNumber)
98 elif mvcc_dev_info.nTLayerType == MV_GENTL_XOF_DEVICE:
99 print (
"\nXoF device: [%d]" % i)
100 strModeName =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chModelName)
101 print (
"device model name: %s" % strModeName)
103 strSerialNumber =
decoding_char(mvcc_dev_info.SpecialInfo.stXoFInfo.chSerialNumber)
104 print (
"user serial number: %s" % strSerialNumber)
108 for i
in range(0, interfaceList.nInterfaceNum):
109 interfaceInfo = cast(interfaceList.pInterfaceInfos[i], POINTER(MV_INTERFACE_INFO)).contents
110 print(
"interface: [%d]" % i)
113 print(
"display name: %s" % displayName)
116 print(
"serial number: %s" % serialNumber)
119 print(
"model name: %s" % modelName)
122 print(
"interface id: %s" % interfaceId)
125 if __name__ ==
"__main__":
129 MvCamera.MV_CC_Initialize()
131 SDKVersion = MvCamera.MV_CC_GetSDKVersion()
132 print (
"SDKVersion[0x%x]" % SDKVersion)
134 interfaceList = MV_INTERFACE_INFO_LIST()
135 transportLayerType = MV_GIGE_INTERFACE | MV_CAMERALINK_INTERFACE | MV_CXP_INTERFACE | MV_XOF_INTERFACE
138 ret = MvCamera.MV_CC_EnumInterfaces(transportLayerType, interfaceList)
140 print(
"enum interfaces fail! ret[0x%x]" % ret)
143 if interfaceList.nInterfaceNum == 0:
144 print(
"find no interface!")
147 print(
"Find %d interfaces!" % interfaceList.nInterfaceNum)
150 nInterfaceIndex =
input_func(
"please input the number of the interface to connect:")
152 if int(nInterfaceIndex) >= interfaceList.nInterfaceNum:
153 print(
"input error!")
157 cam_instance = MvCamera()
158 interface_instance = MvCamera()
161 curInterface = cast(interfaceList.pInterfaceInfos[int(nInterfaceIndex)], POINTER(MV_INTERFACE_INFO)).contents
163 ret = interface_instance.MV_CC_CreateInterface(curInterface)
165 raise Exception(
"create interface handle fail! ret[0x%x]" % ret)
168 ret = interface_instance.MV_CC_OpenInterface()
170 raise Exception(
"open interface fail! ret[0x%x]" % ret)
172 print(
"open interface success")
175 deviceList = MV_CC_DEVICE_INFO_LIST()
176 ret = interface_instance.MV_CC_EnumDevicesByInterface(deviceList)
178 raise Exception(
"enum devices fail! ret[0x%x]" % ret)
180 if deviceList.nDeviceNum == 0:
181 raise Exception(
"find no device!")
185 nCamIndex =
input_func(
"please input the number of the device to connect:")
187 if int(nCamIndex) >= deviceList.nDeviceNum:
188 raise Exception(
"input error!")
191 stDeviceList = cast(deviceList.pDeviceInfo[int(nCamIndex)], POINTER(MV_CC_DEVICE_INFO)).contents
193 ret = cam_instance.MV_CC_CreateHandle(stDeviceList)
195 raise Exception(
"create handle fail! ret[0x%x]" % ret)
198 ret = cam_instance.MV_CC_OpenDevice(MV_ACCESS_Exclusive, 0)
200 raise Exception(
"open device fail! ret[0x%x]" % ret)
203 if stDeviceList.nTLayerType == MV_GIGE_DEVICE
or stDeviceList.nTLayerType == MV_GENTL_GIGE_DEVICE:
204 nPacketSize = cam_instance.MV_CC_GetOptimalPacketSize()
205 if int(nPacketSize) > 0:
206 ret = cam_instance.MV_CC_SetIntValue(
"GevSCPSPacketSize", nPacketSize)
208 print(
"Warning: Set Packet Size fail! ret[0x%x]" % ret)
210 print(
"Warning: Get Packet Size fail! ret[0x%x]" % nPacketSize)
213 ret = cam_instance.MV_CC_SetEnumValue(
"TriggerMode", MV_TRIGGER_MODE_OFF)
215 raise Exception(
"set trigger mode fail! ret[0x%x]" % ret)
218 ret = cam_instance.MV_CC_RegisterImageCallBackEx(CALL_BACK_FUN,
None)
220 raise Exception(
"register image callback fail! ret[0x%x]" % ret)
223 ret = cam_instance.MV_CC_StartGrabbing()
225 raise Exception(
"start grabbing fail! ret[0x%x]" % ret)
227 print (
"press Enter key to stop grabbing.")
231 ret = cam_instance.MV_CC_StopGrabbing()
233 raise Exception(
"stop grabbing fail! ret[0x%x]" % ret)
236 ret = cam_instance.MV_CC_CloseDevice()
238 raise Exception(
"close deivce fail! ret[0x%x]" % ret)
241 cam_instance.MV_CC_DestroyHandle()
244 interface_instance.MV_CC_CloseInterface()
247 interface_instance.MV_CC_DestroyInterface()
248 except Exception
as e:
250 cam_instance.MV_CC_CloseDevice()
251 cam_instance.MV_CC_DestroyHandle()
252 interface_instance.MV_CC_CloseInterface()
253 interface_instance.MV_CC_DestroyInterface()
257 MvCamera.MV_CC_Finalize()